package Servlet;

import com.mysql.cj.jdbc.MysqlDataSource;
import jdbc_demo.my;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;

@WebServlet(name = "MyServlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        System.out.println("当前输入为" + name+":"+password);
        if(my.compare(name,password)==1) {  //调用对象my.java , 为1时可执行

            String username = req.getParameter("name");    //登录数据库的匹配用户名称
            req.getSession().setAttribute("username",username);

            System.out.println("info:Success!");
            resp.getWriter().write("<script>alert('登录成功！');window.location='http://localhost:8080/showInfo.jsp';</script>");
        } else {
            System.out.println("info:Failed!");
            resp.getWriter().write("<script>alert('登录失败！');window.location='http://localhost:8080/index.html';</script>");
        }

    }
}
